﻿namespace mei_cs.comm;

using System.Configuration;

// ConfigurationManager使用反射实例化，此处禁用类未实例化告警
// ReSharper disable once ClassNeverInstantiated.Global
public class ConnectSection : ConfigurationSection
{
    [ConfigurationProperty(nameof(Host), IsRequired = true)]
    public string Host => (string)this[nameof(Host)];

    [ConfigurationProperty(nameof(Port), IsRequired = true)]
    public int Port => (int)this[nameof(Port)];

    [ConfigurationProperty(nameof(AsyncPort), IsRequired = true)]
    public int AsyncPort => (int)this[nameof(AsyncPort)];
}

public static class Config
{
    private static readonly string BasePath = Path.Combine(Directory.GetCurrentDirectory(), "data");

    public static string DataPath(params string[] paths)
    {
        var p = BasePath;
        foreach (var path in paths)
        {
            // 拼接路径
            p = Path.Combine(p, path);

            // 如果路径不存在，则创建目录
            if (!Directory.Exists(p))
            {
                Directory.CreateDirectory(p);
            }
        }

        return p;
    }

    public static T GetConfig<T>() where T : ConfigurationSection
    {
        return (T)ConfigurationManager.GetSection(typeof(T).Name);
    }
}